import 'package:common/export/bloc.dart';
import 'package:common/export/tools.dart';
import 'package:components/lara_press_text.dart';
import 'package:flutter/material.dart';
import 'package:larapress_app/navigation/navigation_cubit.dart';
import 'package:larapress_app/navigation/navigation_state.dart';
import 'package:larapress_app/pages/home/home_page.dart';
import 'package:user/pages/user_main.dart';

class Navigation extends StatelessWidget {
  const Navigation({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => NavigationCubit(),
      child: Builder(builder: (context) => _buildPage(context)),
    );
  }

  Widget _buildPage(BuildContext context) {
    TextStyle labelStyle = TextStyle(fontSize: 12.sp, letterSpacing: 2);
    NavigationCubit cubit = context.read<NavigationCubit>();
    return Scaffold(
      body: BlocBuilder<NavigationCubit, NavigationState>(
        builder: (context, state) {
          return PageView(
            controller: state.pageController,
            onPageChanged: (index) {
              cubit.onPageChanged(index);
            },
            children: [LaraPressHomePage(), LaraPressUserMainPage()],
          );
        },
      ),
      bottomNavigationBar: BlocBuilder<NavigationCubit, NavigationState>(
        builder: (context, state) {
          return BottomNavigationBar(
            unselectedLabelStyle: labelStyle,
            selectedLabelStyle: labelStyle.copyWith(
              color: Theme.of(context).primaryColor,
            ),
            onTap: (int index) {
              cubit.onPageChanged(index);
            },
            items: state.tabs
                .map(
                  (e) => BottomNavigationBarItem(
                    activeIcon: LaraPressIcon(
                      icon: e['activeIcon'],
                      color: Theme.of(context).primaryColor,
                      size: 20.sp,
                    ),
                    icon: LaraPressIcon(icon: e['icon'], size: 20.sp),
                    label: e['name'],
                  ),
                )
                .toList(),
          );
        },
      ),
    );
  }
}
